package com.realjinge.controller.admin;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.realjinge.entity.Question;
import com.realjinge.entity.Type;
import com.realjinge.service.AnswerService;
import com.realjinge.service.QuestionService;
import com.realjinge.service.TypeService;
import com.realjinge.utils.ErrorCodeUtils;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource
	private TypeService typeService;
	@Resource
	private QuestionService questionService;
	@Resource
	private AnswerService answerService;

	/**
	 * 后台管理-首页&题目列表
	 * 
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/adminIndex.do", method = { RequestMethod.GET, RequestMethod.POST })
	public String toQuestionList(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (username != null && password != null) {
			if (("admin".equals(username) && "admin".equals(password))
					|| ("realjinge".equals(username) && "realjinge".equals(password))) {
				List<Type> list = typeService.selectTypeList();
				map.put("typeList", list);
				List<Question> questions = questionService.selectQuestionList();
				map.put("questionList", questions);
				return "view/admin/questionList";
			} else {
				map.put("msg", "登录失败");
			}
		}
		// return "redirect:adminIndex.do";
		return "view/admin/index";
	}

	@RequestMapping(value = "/updateQuestionStatus.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void updateQuestionStatus(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		String status = request.getParameter("status");
		// String id = request.getParameter("status");
		String questionNo = request.getParameter("questionNo");
		if (status != null && !"".equals(status) && questionNo != null && !"".equals(questionNo)) {
			if ("01".equals(status) || "02".equals(status)) {
				// update status
				// questionService.updateQuestionStatusBYID(status,
				// Integer.parseInt(id));
				int ret = questionService.updateQuestionStatusBYQuestionNo(status, questionNo);
				if (1 == ret) {
					out(response, ErrorCodeUtils.errCode0());
				} else {
					out(response, ErrorCodeUtils.errCode1());
				}
			} else {
				out(response, ErrorCodeUtils.errCode1());
			}
		} else {
			out(response, ErrorCodeUtils.errCode1());
		}
	}

	/**
	 * 输出JSON字符串
	 * 
	 * @param response
	 * @param jsonMsg
	 */
	protected void out(HttpServletResponse response, String jsonMsg) {

		try {
			response.setContentType("text/json;charset=utf-8");
			response.getWriter().write(jsonMsg);
		} catch (IOException e) {
			// do nothing
		}

	}

}
